from django.http import HttpResponse
import requests
import re

class X23usCom:

    def __init__ (self,key_word):

        self.key_word = key_word
    
    def index(self):
        key_word = self.key_word.encode('gb2312')
        #key_word = urllib.parse.quote(key_word)
        url = 'https://www.x23us.com/modules/article/search.php'
        params = {
            'searchtype':'keywords',
            'searchkey':key_word,
            }
        
        res = requests.get(url,params=params)
        res.encoding = 'gbk'
        if res.history:
            return {'url':res.url}
        else:        
            return res

    def info(self):
        
        html = self.index().text
        novel_name = '<td class="odd"><a href=".*?">(.*?)</a></td>'
        novel_url = '<td class="even"><a href="(.*?)" target="_blank">.*?</a></td>'
        novel_author = '<td class="odd">\w*?([^a]*?)</td>'
        novel_statu = '<td class="even" align="center">(.*?)</td>'
        refresh_time = '<td class="odd" align="center">(.*?)</td>'
        novel_words = '<td class="even">(\w*?)</td>'
        novel_author_url = None
        latest_chapter = None
        latest_chapter_url = None
        novel_type = None
        novel_img = None
        
        novel_name = re.findall(novel_name,html)
        #print(book_name)
        novel_url = re.findall(novel_url,html)
        #print(book_url)
        novel_author = re.findall(novel_author,html)
        #print(book_author)
        novel_statu = re.findall(novel_statu,html)
        #print(book_statu)
        novel_words = re.findall(novel_words,html)
        refresh_time = re.findall(refresh_time,html)
        info_dict = {}
        for i in range(len(novel_name)):
            info_dict[novel_name[i].replace('<b style="color:red">','').replace('</b>','')] = {'novel_author':novel_author[i].replace('<b style="color:red">','').replace('</b>',''),
                                                                                               'novel_author_url':novel_author_url,
                                                                                               'novel_url':novel_url[i],
                                                                                               'latest_chapter':latest_chapter,
                                                                                               'latest_chapter_url':latest_chapter_url,
                                                                                               'novel_type': novel_type,
                                                                                               'refresh_time': refresh_time[i],
                                                                                               'novel_statu':novel_statu[i],
                                                                                               'novel_words':novel_words[i],
                                                                                               'novel_img': novel_img,
                                                                                               }
        return info_dict
        

        

        

        
if __name__ =='__main__':

    x = index()

    
























    
